﻿@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Common.Common
@using ZBJF.Warranty.Common.Enums.InfoCollect
@using ZBJF.Warranty.Common.Enums.InsuranceProducts
@using ZBJF.Warranty.Common.Enums.InsuranceWarranty
@using ZBJF.Warranty.Model
@using EnumHelper = ZBJF.Warranty.Common.Common.EnumHelper
@model ZBJF.Warranty.Model.InsuranceWarrantyRecord
<!--远程分拣信息收集-->
@*@RenderPage("~/Views/WorkOrderCommonInfo/InfoCollectItems.cshtml", new { CollectWithType = (byte)CollectWithType.RemotePartitionCollect })*@
<!--远程分拣审核-->
@*@RenderPage("~/Views/WorkOrderCommonInfo/AuditInfo.cshtml", new { AuditProjectType = (byte)AuditProjectType.RemoteSorting })*@
<!--通融安排与历史-->
@RenderPage("~/Views/WorkOrderCommonInfo/BendTheRuleRecordForm.cshtml", new { BendTheRulesWith = (byte)BendTheRulesWithType.RemotePartitionBendTheRules })
@RenderPage("~/Views/WorkOrderCommonInfo/InsuranceWarrantyBendTheRulesRecordInfo.cshtml", new { BendTheRulesWith = (byte)BendTheRulesWithType.RemotePartitionBendTheRules })
<h4>维修安排</h4>
<label>选择维修方式</label>
@Html.DropDownListFor(m => m.WarrantyMaintenanceTypeID, ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(WarrantyMaintenanceType), (byte)WarrantyMaintenanceType.SendRevised))
<p><label style="width: 95%;">请在下方输入客户地址信息:</label><input type="button" value="呼叫快递" id="CallCurier" /></p>
<table class="table table-responsive table-bordered table-hover">
    <tr>
        <th class="w120">报案人：</th>
        <td class="w-p25">@Html.DisplayFor(o => o.Reporter, new { maxlength = 15 })</td>
        <th class="w120">报案人电话：</th>
        <td class="w-p25">@Html.DisplayFor(o => o.ReporterPhone, new { maxlength = 15 })</td>
    </tr>
    <tr>
        <th class="w120">报案人地址：</th>
        <td colspan="3">@Html.TextBoxFor(o => o.AccidentAddress, new { @class = "w-p98", maxlength = 100 })</td>
    </tr>
</table>
<div id="sortingCenterMain">
    <h5>请在下方选定分拣中心</h5>
    <label>所在省</label>
    @Html.DropDownList("SortingCenterBelongProvinceID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.D_ProvinceConvertToSelectList(), new { @class = "Province" })
    <label>所在市</label>
    <select name="SortingCenterBelongCityID" id="SortingCenterBelongCityID">
        <option>先选择省份</option>
    </select>
    <input type="button" class="btn" value="重置" onclick="resetSortingCenter()" />
    <input type="button" class="btn btn-primary" value="查询" onclick="searchSortingCenter()" />
    <table class="table table-responsive table-bordered table-hover" id="tabSortingCenter">
        <thead>
            <tr>
                <th>选择</th>
                <th>中心名称</th>
                <th>所在省</th>
                <th>所在市</th>
                <th>详细地址</th>
                <th>联系人</th>
                <th>联系电话</th>
            </tr>
        </thead>
    </table>
    <form id="fromExpressInfo" enctype="multipart/form-data" method="post">
        @{
            var insuranceWarrantyRecordExpressInfo = Model.InsuranceWarrantyRecordExpressInfo.FirstOrDefault(o => o.ExpressSendByWhoType == (byte)ExpressSendByWhoType.UserSendToPartitionCenter) ?? new InsuranceWarrantyRecordExpressInfo();
        }
        @Html.HiddenFor(m => m.SortingCenterID)
        @Html.HiddenFor(m => m.ServiceProviderBranchId)
        <input type="hidden" name="insuranceWarrantyRecordID" value="@Model.ID" />
        <h4 style="width: 90%; float: left;">物流信息</h4>
        <p>
            <input type="button" value="获取报价" /><input type="button" value="快递下单" id="DeliveryOrder" />
        </p>
        <table class="table table-responsive table-bordered table-hover">
            <tr>
                <th class="w120">寄件人：</th>
                <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserName, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.SendUserName) ? Model.Reporter : insuranceWarrantyRecordExpressInfo.SendUserName, validate = "{CanNotContainsSpecialChar:true,required:true,messages:{required:'寄件人不能为空'}}" })</td>
                <th class="w120">寄件人电话：</th>
                <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserPhone, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.SendUserPhone) ? Model.ReporterPhone : insuranceWarrantyRecordExpressInfo.SendUserPhone, validate = "{isTelphone:true,required:true,messages:{required:'寄件人电话不能为空'}}" })</td>
            </tr>
            <tr>
                <th>寄件人地址：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserAddress, new { validate = "{required:true,messages:{required:'寄件人地址不能为空'}}" })</td>
                <th>收件人：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserName, new { validate = "{required:true,messages:{required:'收件人不能为空'}}" })</td>
            </tr>
            <tr>
                <th>收件人电话：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserPhone, new { validate = "{isTelphone:true,required:true,messages:{required:'收件人电话不能为空'}}" })</td>
                <th>收件人地址：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserAddress, new { validate = "{required:true,messages:{required:'收件人地址不能为空'}}" })</td>
            </tr>
            <tr>
                <th>快递公司：</th>
                <td>@Html.DropDownListFor(n => insuranceWarrantyRecordExpressInfo.ExpressCompanyID, ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ExpressEnums), 1))</td>
                <th>快递价格：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressPrice, new { maxlength = 7, onkeyup = "onlyInputDecimal(this,2)", validate = "{max:9999.99,number:true}" })</td>
            </tr>
            <tr>
                <th>快递单号：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressNo, new { validate = "{isRightfulStringNounderline:true,required:true,messages:{required:'快递单号不能为空'}}" })</td>
                <th>取件快递员手机：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.PickUpExpressManPhone, new { validate = "{isTelphone:true}" })</td>
            </tr>
            <tr>
                <th>取件快递员：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.PickUpExpressManName)</td>
                <th>物流状态：</th>
                <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressStatus)</td>
            </tr>
        </table>
        <h4>分拣备注</h4>
        @Html.TextAreaFor(n => n.SortingRemark, new { @class = "w-p98", placeholder = "请填写分拣备注" })
    </form>
</div>
<div id="serviceProviderBranchMain">
    <div class="search-panel">
        <h5>维修安排：请在下方选定要维修的配件以及选定一家需使用的服务商</h5>
        <label>配件</label>
        @Html.DropDownList("AccessoriesTypeID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList(Model.InsuranceOrder.ProductTypeId??0,addEmptySelect: false))
        <label>配件品质</label>
        @Html.DropDownList("AccessoriesLevelID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.AccessoriesLevelConvertToSelectList(addEmptySelect: false))
        <label>所在省</label>@Html.DropDownList("ProviderBranchProvinceID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.D_ProvinceConvertToSelectList(addEmptySelect: false), new { @class = "Province" })
        <label>所在市</label>
        <select name="ProviderBranchCityID" id="ProviderBranchCityID"></select>
        <input type="button" class="btn btn-primary" value="查询" onclick="searchProviderBranch()" />
    </div>
    <table class="table table-responsive table-bordered table-hover" id="tabServiceProviderBranch">
        <thead>
            <tr>
                <th>选择</th>
                <th>所属服务商</th>
                <th>网点名称</th>
                <th>型号</th>
                <th>维修报价</th>
                <th>所在省</th>
                <th>所在市</th>
                <th>详细地址</th>
            </tr>
        </thead>
    </table>
</div>
<div id="userWarrantyRemarkMain">
    <h4>维修备注</h4>
    @Html.TextAreaFor(n => n.UserWarrantyRemark, new { @class = "w-p98", placeholder = "请填写维修备注" })
</div>
@if (ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetParentStatusCode(Model.Status) == (byte)InsuranceWarrantyStatus.Processing)
{
    <div class="form-actions text-center">
        <input class="btn btn-primary Edit-R" type="button" onclick="Save(1)" value="保存信息">
        <input class="btn btn-primary Edit-R" type="button" onclick="Save(2)" value="提交维修">
        <input class="btn btn-primary Edit-R" type="button" value="拒赔" onclick="showIWRRefuseLayer()">
        <input class="btn" type="button" value="取消报案" id="btnCancel">
    </div>
}
<script src="~/Scripts/uploadAdapter/jquery.loadscript.js"></script>
<script src="~/Scripts/uploadAdapter/jquery.uploadAdapter.js"></script>
<script src="~/Scripts/WarrantyRecord/warrantyRecord.js"></script>
<script type="text/javascript">
    $(function() {
        changeMaintenanceType($("#WarrantyMaintenanceTypeID"));//切换维修方式
        //加载分拣中心
        $("#tabSortingCenter").dataTable({
            "sAjaxSource": "/WorkOrderCommonInfo/SortingCenterList",
            aoColumns: [
                {
                    "mData": null,
                    "bSortable": false,
                    "fnRender": function (nRow) {
                        return '<input type="radio" name="SortingCenter" class="CheckBoxItem" '+("@(Model.SortingCenterID)"==nRow.aData.ID?"checked":"") +' value="'+nRow.aData.ID+'" onclick="checkSortingCenter(this)"/>';
                    }
                },
                { "mData": "SortingCenterName", "bSortable": false },
                { "mData": "ProvinceName", "bSortable": false },
                { "mData": "CityName", "bSortable": false },
                { "mData": "Address", "bSortable": false },
                { "mData": "ContactName", "bSortable": false },
                { "mData": "ContactPhone", "bSortable": false }
            ]
        });

        $("#tabServiceProviderBranch")
            .dataTable({
                "sAjaxSource": "/WorkOrderCommonInfo/ServiceProviderBranchList?AccessoriesTypeID="+$("#AccessoriesTypeID").val()+"&AccessoriesLevelID="+$("#AccessoriesLevelID").val()+"&provinceId="+$("#ProviderBranchProvinceID").val()+"&CityId="+$("#ProviderBranchCityID").val()+"&ServiceType=" + $("#WarrantyMaintenanceTypeID").val() + "&ProductModelId=@Model.InsuranceOrder.ProductModelId",
                aoColumns: [
                    {
                        "mData": null,
                        "bSortable": false,
                        "fnRender": function (nRow) {
                            return '<input type="radio" name="ServiceProviderBranch" class="CheckBoxItem" '+("@(Model.ServiceProviderBranchId)"==nRow.aData.ServiceProviderBranchID?"checked":"") +' value="'+nRow.aData.ServiceProviderBranchID+'" onclick="checkServiceBranch(this)"/>';
                        }
                    },
                    { "mData": "ServiceProviderName", "bSortable": false },
                    { "mData": "ServiceProviderBranchName", "bSortable": false },
                    { "mData": "QuotationPrice", "bSortable": false },
                    { "mData": "ModelName", "bSortable": false },
                    { "mData": "ProvinceName", "bSortable": false },
                    { "mData": "CityName", "bSortable": false },
                    { "mData": "Address", "bSortable": false }
                ]
            });
    });

    $("#WarrantyMaintenanceTypeID").change(function() {
        changeMaintenanceType(this);
        searchProviderBranch();
    });
    function changeMaintenanceType(obj) {
        var warrantyMaintenanceType = $(obj).val()-0;
        if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.SendRevised)") {
            $("#sortingCenterMain").show();//分拣中心信息
            $("#CallCurier").show();//呼叫快递
            $("#userWarrantyRemarkMain").hide();//维修备注
            $("#serviceProviderBranchMain").hide();//服务网点
        } else if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.OfficialRepair)") {
            $("#sortingCenterMain").hide();
            $("#CallCurier").hide();
            $("#userWarrantyRemarkMain").show();
            $("#serviceProviderBranchMain").hide();
        }else{
            $("#sortingCenterMain").hide();
            $("#CallCurier").hide();
            $("#userWarrantyRemarkMain").show();
            $("#serviceProviderBranchMain").show();
        }
    }

    //保存信息
    function Save(saveType) {
        var fromExpressInfo = $('#fromExpressInfo').serializeJson();//快递信息
        var auditInfoForm = $('#auditInfoForm').serializeJson();//审核信息
        var param = $.extend({}, fromExpressInfo,auditInfoForm);//多个表单参数拼接

        var sortingCenterID = $("#SortingCenterID").val();
        var warrantyMaintenanceType = $("#WarrantyMaintenanceTypeID").val() - 0;
        var accidentAddress = $("#AccidentAddress").val();
        var obj = $("#tabServiceProviderBranch").find(":radio:checked");
        var serviceProviderBranchId= $("#ServiceProviderBranchId").val();
        var serviceProviderName= $(obj).parent().siblings().eq(0).html();
        var serviceProviderBranchName= $(obj).parent().siblings().eq(1).html();
        var quotationPrice= $(obj).parent().siblings().eq(2).html();
        var userWarrantyRemark = $("#UserWarrantyRemark").val();
        var accessoriesTypeID = $("#AccessoriesTypeID").val();
        var accessoriesLevelID = $("#AccessoriesLevelID").val();

        param.Status = "@Model.Status";
        param.SortingCenterID = sortingCenterID;
        param.WarrantyMaintenanceTypeID = warrantyMaintenanceType;
        param.AccidentAddress = accidentAddress;
        param.ServiceProviderBranchId = serviceProviderBranchId;
        param.ServiceProviderName = serviceProviderName;
        param.ServiceProviderBranchName = serviceProviderBranchName;
        param.UserWarrantyRemark = userWarrantyRemark;
        param.AccessoriesTypeID = accessoriesTypeID;
        param.AccessoriesLevelID = accessoriesLevelID;
        if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.SendRevised)") {
            if (sortingCenterID == "") {
                layer.alert("请选择远程分拣中心");
                return;
            }
        }
        else if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.OnsiteRepair)"||warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.ShopRepair)") {
            if (serviceProviderBranchId == "") {
                layer.alert("请选择维修服务商");
                return;
            }
            if (saveType == 2 &&parseFloat( quotationPrice)>parseFloat("@Model.InsuranceOrder.InsuredAmount")) {//验证报价是否超过保额
                layer.alert("维修报价大于保额@(Model.InsuranceOrder.InsuredAmount)，请选择其他维修商");
                return;
            }
        }
        if (saveType==2) {//提交维修
            if (accidentAddress == "") {
                layer.alert("请填写报案人地址");
                return;
            }
            //验证信息收集
            var infoCollectComplete = true;
            $(".InfoCollectCanUploadPicCount").each(function() {
                //需要上传的图片数大于已经上传的图片数则视为没有完成信息收集
                if ($(this).text() >
                    $("#" + $(this).attr("id").replace("InfoCollectCanUploadPicCount", "InfoCollectHadUploadPicCount"))
                    .text())
                    infoCollectComplete = false;
            });
            if (!infoCollectComplete) {
                layer.alert("请完成信息收集！");
                return;
            }
            //验证审核信息
            if (!$("#auditInfoForm").valid()) {
                layer.alert("请选择审核结果！");
                return;
            }
            //验证物流信息
            if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.SendRevised)" &&
                !$("#fromExpressInfo").valid()) {
                layer.alert("请填写完整物流信息！");
                return;
            }
        }

        layer.load();
        $.ajax({
            url: "/ToBeMaintainWorkOrder/SaveRemoteAuditInsuranceWarrantyRecord?ID=@Model.ID"+"&saveType="+saveType,
            type: "Post",
            data: param,
            success: function(data) {
                layer.alert(data.Message, {
                    end: function () {
                        layer.closeAll();
                        if (data.Success) {
                            layer.load();
                            location.reload();
                        }
                    },
                    yes: function () {
                        layer.closeAll();
                        if (data.Success) {
                            layer.load();
                            location.reload();
                        }
                    }
                });
            }
        });
    }

    var index;
    //取消报案
    $("#btnCancel").click(function() {
        index = layer.open({
            type: 1,
            title: '请选择取消报案原因', //标题栏
            closeBtn: false,
            area: '600px;',
            shade: 0.8,
            id: 'LAY_layuipro', //设定一个id，防止重复弹出
            resize: false,
            btn: ['确认', '取消'],
            btnAlign: 'c', //按钮排列
            yes: function(index, layero) {
                UpdateInsuranceWarrantyRecordStatus();
            },
            moveType: 1, //拖拽模式，0或者1
            content: $('#showCancelReport').show()
        });
    });
    //取消报案点击确定后修改状态
    function UpdateInsuranceWarrantyRecordStatus() {
        var statusReasonId = $("#StatusReasonCancel").val();
        if (!statusReasonId) {
            layer.alert("请选择取消报案原因");
        }
        var stauts = @((byte) InsuranceWarrantyStatus.Canceled);
        $.ajax({
            url: "/ToBeMaintainWorkOrder/UpdateInsuranceWarrantyRecordStatus?ID=" + @Model.ID,
            dataType: "json",
            type: "post",
            data: { statusReasonId: statusReasonId, status: stauts },
            success: function(data) {
                if (data.Result) {
                    layer.close(index);
                    layer.alert(data.Message);
                    return;
                }
            }
        });
    }

    function showIWRRefuseLayer() {
        layer.open({
            type: 2,
            title: '请选择拒赔原因',
            shadeClose: true,
            shade: 0.8,
            area: ['820px', '400px'],
            content: '/ToBeMaintainWorkOrder/IWRRefuse?insuranceWarrantyRecordId=@Model.ID'+'&infoCollectWithType=@((byte)CollectWithType.RemotePartitionCollect)'+'&auditProjectType=@((byte)AuditProjectType.RemoteSorting)'
        });
    }

    $("#DeliveryOrder").click(function() {
        debugger;
        var sendUserName = $("#insuranceWarrantyRecordExpressInfo_SendUserName").val();
        if (!sendUserName) {
            layer.alert("请填写寄件人!");
            return false;
        }
        var sendUserPhone = $("#insuranceWarrantyRecordExpressInfo_SendUserPhone").val();
        if (sendUserPhone == null||sendUserPhone=="") {
            layer.alert("请填写寄件人电话!");
            return false;
        }
        var sendUserAddress = $("#insuranceWarrantyRecordExpressInfo_SendUserAddress").val();
        if (sendUserAddress == null||sendUserAddress=="") {
            layer.alert("请填写寄件人地址!");
            return false;
        }
        var receiverUserName = $("#insuranceWarrantyRecordExpressInfo_ReceiverUserName").val();
        if (receiverUserName == null||receiverUserName=="") {
            layer.alert("请填写收件人!");
            return false;
        }
        var receiverUserPhone = $("#insuranceWarrantyRecordExpressInfo_ReceiverUserPhone").val();
        if (receiverUserPhone == null||receiverUserPhone=="") {
            layer.alert("请填写收件人电话!");
            return false;
        }
        var receiverUserAddress = $("#insuranceWarrantyRecordExpressInfo_ReceiverUserAddress").val();
        if (receiverUserAddress == null||receiverUserAddress=="") {
            layer.alert("请填写收件人地址!");
            return false;
        }
        var expressCompanyId =parseInt($("#insuranceWarrantyRecordExpressInfo_ExpressCompanyID").val());
        if (expressCompanyId==0) {
            layer.alert("请选择快递公司!");
            return false;
        }
        var expressPrice =Number($("#insuranceWarrantyRecordExpressInfo_ExpressPrice").val());
        if (expressPrice==0||isNaN(expressPrice)) {
            layer.alert("请填写快递价格!");
            return false;
        }
        var fromExpressInfo=$('#fromExpressInfo').serialize();//快递信息
        $.ajax({
            url: "/ShunFengApi/AddNewOrder?insuranceWarrantyRecordId="+@Model.ID,
            type: "Post",
            data: fromExpressInfo,
            success: function(data) {
                //layer.alert(data.message, {
                //    end:function() {
                //        layer.closeAll();
                //    },
                //    yes: function() {
                //        layer.closeAll();
                //        location.reload();
                //    }
                //});
            }
        });
    });
</script>
